<template>
  <div class="brand_page">

    <div class="row">
      <h3>错位图标题编辑</h3>
      <textarea id="editor1" class="ckeditor" rows="10" cols="80">
        ......
      </textarea>
    </div>
    <div class="row top_magin">
      <h3>错位图编辑（4张）</h3>
      <input type="button" class="btn btn-info" @click="addBigImg()" value="新增大图">
      <div class="row_height">
        <div class="col-md-3" v-for="(item,index) in main_list" :key="index">
          <div class="img_box">
            <img :src="item.img_url" alt="" class="full-width">
          </div>
          <p>索引：{{item.index}}</p>
          <div class="btn_box">
            <input type="button" value="删除" @click="deleteData(item)" class="btn btn-danger">
          </div>
        </div>
      </div>
    </div>
    <div class="row top_magin">
      <h3>错位图下面文本编辑</h3>
      <textarea id="editor2" class="ckeditor" rows="10" cols="80">
        ......
      </textarea>
      <el-button type="primary" style="margin:20px 0 0 0" @click="sub_form()">提交</el-button>
    </div>
    <!--新增弹窗代码-->
    <el-dialog title="新增" :visible.sync="dialog_show">
      <el-form enctype="multipart/form-data" id="uploadForm">
        <el-form-item label="选择图片" label-width="120px">
          <input type="file" name="add_pic" id="add_img">
        </el-form-item>
        <el-form-item label="图片排序" label-width="120px">
          <el-input auto-complete="off" v-model="pic_index" type="number" name="pic_index"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_show = false">取 消</el-button>
        <el-button type="primary" @click="uploadData()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import g from '../../components/global.js'
export default {
  data() {
    return {
      big_img: '',
      dialog_show: false,
      pic_index: '',
      main_list: []
    }
  },
  mounted() {
    CKEDITOR.replace('editor1')
    CKEDITOR.replace('editor2')
    this.get_img()
    this.get_data()
  },
  methods: {
    uploadData() {
      var that = this
      var formData = new FormData($('#uploadForm')[0])
      $.ajax({
        url: g.res_url + '/api/v1/brand_img_cw',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
          if (data == 'success') {
            that.$message({
              message: '操作成功！',
              type: 'success'
            })
            that.get_img()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(returndata) {
          that.$message.error('网络繁忙！')
        }
      })
      that.dialog_show = false
    },
    get_img() {
      var _this = this
      $.ajax({
        type: 'post',
        url: g.res_url + '/api/v1/get_img_cw',
        success: function(data) {
          var dataObjs = JSON.parse(data)
          _this.main_list = dataObjs.data
        }
      })
    },
    addBigImg() {
      this.dialog_show = true
    },
    sub_form() {
      var _this = this
      var formtxt1 = CKEDITOR.instances.editor1.getData()
      var formtxt2 = CKEDITOR.instances.editor2.getData()
      $.ajax({
        type: 'post',
        url: g.res_url + '/api/v1/brand_save_txt',
        data: {
          md_tle: formtxt1,
          bot_tle: formtxt2
        },
        success: function(data) {
          if (data) {
            _this.$message.success('操作成功')
          } else {
            _this.$message.error('操作失败')
          }
        },
        error: function() {
          _this.$message.error('网络错误')
        }
      })
    },
    get_data() {
      $.ajax({
        url: g.res_url + '/api/v1/brand_getData',
        type: 'post',
        success: function(data) {
          var dataObjs = JSON.parse(data)
          var dataObj = dataObjs.data[0]
          setTimeout(function() {
            CKEDITOR.instances.editor1.setData(dataObj.md_tle)
            CKEDITOR.instances.editor2.setData(dataObj.bot_tle)
          }, 1000)
        }
      })
    },
    deleteData(item) {
      var _this = this
      $.ajax({
        url: g.res_url + '/api/v1/brand_del',
        type: 'post',
        data: {
          img_id: item.id
        },
        success: function(data) {
          if (data == 'success') {
            _this.$message.success('操作成功！')
            _this.get_img()
          } else {
            _this.$message.error('操作失败！')
          }
        },
        error: function() {
          _this.$message.error('网络繁忙！')
        }
      })
    }
  }
}
</script>
<style scoped>
.row_height {
  height: 150px !important;
  margin: 28px 0;
}
.btn_box {
  margin-left: 45px;
}
.img_box {
  width: 200px;
  height: 200px;
  overflow: hidden;
  margin-bottom: 20px;
}
.full-width {
  width: 100%;
}
.col-md-4 {
  margin-top: 30px;
}
.brand_page {
  padding: 0 0 50px 20px;
}
.top_magin {
  margin-top: 50px !important;
}
</style>

